package com.sxxm.qxgl.utils;

import com.sxxm.qxgl.dto.Meta;
import com.sxxm.qxgl.dto.SysDept;
import com.sxxm.qxgl.dto.SysOperate;
import com.sxxm.qxgl.dto.UserMenuVo;
import com.sxxm.qxgl.entity.UserMenu;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils {
    public static List createDeptTree(List<SysDept> data, int parent) {
        if(data.size()==0){
            return null;
        }
        List<SysDept> children = new ArrayList<>();
        for(SysDept tree:data){
            if(tree.getParentId()==parent){
                List<SysDept> children1 = new ArrayList<>(data);
                children1.remove(tree);
                tree.setChildren(createDeptTree(children1,tree.getDeptId()));
                children.add(tree);
            }
        }
        return children;
    }

    public static List createOperateTree(List<SysDept> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<SysDept> children = new ArrayList<>();
        for(SysDept tree:data){
            if(tree.getParentId().equals(menuId)){
                List<SysDept> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                tree.setChildren(createOperateTree(childrenData,String.valueOf(tree.getDeptId())));
                children.add(tree);
            }
        }
        return children;
    }

    public static List getUserMenuTree(List<UserMenu> data, Long menuId) {
        if(data.size()==0){
            return null;
        }
        List<UserMenuVo> children = new ArrayList<>();
        for(UserMenu tree:data){
            if(tree.getParentId().equals(menuId)){
                List<UserMenu> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                UserMenuVo userMenuVo = new UserMenuVo();
                userMenuVo.setPath(tree.getPath());
                userMenuVo.setId(tree.getMenuId().toString());
                userMenuVo.setMeta(new Meta(tree.getName(),tree.getIcon()));
                userMenuVo.setChildren(getUserMenuTree(childrenData,tree.getMenuId()));
                children.add(userMenuVo);
            }
        }
        return children;
    }
}
